using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using BitMango;
using BitMango.Diagnostics;
using BitMango.Promotion; 
using Holoville.HOTween;
using UnityEngine;
using UnityEngine.SceneManagement;

public static class Scene
{
    static Scene()
    {
        global::Scene._transition = new FadeTransition();
    }

    public static float LoadingProgress { get; private set; }

    public static bool EnableAndroidEscapeEvent
    {
        get
        {
            return global::Scene.enableAndroidEscapeEvent;
        }
        set
        {
            if (value)
            {
                tk2dUIManager instance = tk2dUIManager.Instance;
                if (global::Scene._003C_003Ef__mg_0024cache0 == null)
                {
                    global::Scene._003C_003Ef__mg_0024cache0 = new Action(global::Scene.OnAndroidEscape);
                }
                instance.OnInputUpdate -= global::Scene._003C_003Ef__mg_0024cache0;
                tk2dUIManager instance2 = tk2dUIManager.Instance;
                if (global::Scene._003C_003Ef__mg_0024cache1 == null)
                {
                    global::Scene._003C_003Ef__mg_0024cache1 = new Action(global::Scene.OnAndroidEscape);
                }
                instance2.OnInputUpdate += global::Scene._003C_003Ef__mg_0024cache1;
            }
            else
            {
                tk2dUIManager instance3 = tk2dUIManager.Instance;
                if (global::Scene._003C_003Ef__mg_0024cache2 == null)
                {
                    global::Scene._003C_003Ef__mg_0024cache2 = new Action(global::Scene.OnAndroidEscape);
                }
                instance3.OnInputUpdate -= global::Scene._003C_003Ef__mg_0024cache2;
            }
            global::Scene.enableAndroidEscapeEvent = value;
        }
    }

    public static Camera SceneCamera
    {
        get
        {
            Debugger.Assert(global::Scene.currentScene != null);
            if (global::Scene.currentScene == null)
            {
                return null;
            }
            return global::Scene.GetCamera(global::Scene.currentScene);
        }
    }

    public static Camera PopupCamera
    {
        get
        {
            Debugger.Assert(global::Scene.currentPopup != null);
            if (global::Scene.currentPopup == null)
            {
                return null;
            }
            return global::Scene.GetCamera(global::Scene.currentPopup);
        }
    }

    public static Camera TopOverlayCamera
    {
        get
        {
            Camera result = null;
            float num = 0f;
            foreach (GameObject gameObject in global::Scene._overlayCache.Values)
            {
                if (gameObject.activeSelf)
                {
                    Camera camera = global::Scene.GetCamera(gameObject);
                    if (camera.depth > num)
                    {
                        num = camera.depth;
                        result = camera;
                    }
                }
            }
            return result;
        }
    }

    public static string TransitionName
    {
        get
        {
            return global::Scene._transition.Name;
        }
        set
        {
            if (global::Scene._transition.Name != value)
            {
                Debugger.Assert(global::Scene._transitionDic.ContainsKey(value));
                global::Scene._transition = global::Scene._transitionDic[value];
                UnityEngine.Debug.Log("New transition name - " + global::Scene._transition.Name);
            }
        }
    }

    public static float SceneChangeTime
    {
        get
        {
            return global::Scene.sceneChangeTime;
        }
        set
        {
            global::Scene.sceneChangeTime = Mathf.Max(0f, value);
        }
    }

    public static bool IsLocked { get; private set; }

    public static bool IsPopupClosing { get; private set; }

    public static bool IsPopupShowing
    {
        get
        {
            return global::Scene.PopupShowAnimationCount > 0;
        }
    }

    private static int PopupShowAnimationCount { get; set; }

    public static void ChangeUnityScene(string uSceneName, string sceneName, object param = null, string loadingScene = "Loading")
    {
        global::Scene.ClearQueuePopup();
        global::Scene.ClearPopupStack();
        TaskManager.StartCoroutine(global::Scene.ChangeUnitySceneCoroutine(uSceneName, sceneName, param, loadingScene));
    }

    private static void ClearQueuePopup()
    {
        foreach (global::Scene.PopupItem popupItem in global::Scene._popupStack)
        {
            if (popupItem.queue.Count > 0)
            {
                string text = "Deleted Queue Popup!";
                foreach (global::Scene.PopupItem popupItem2 in popupItem.queue)
                {
                    text = text + " " + popupItem2.popup.name;
                }
                Debugger.Assert(false, text);
                popupItem.queue.Clear();
            }
        }
    }

    private static void ClearPopupStack()
    {
        if (global::Scene._popupStack.Count > 0)
        {
            string text = "UnClosed Popup!";
            foreach (global::Scene.PopupItem popupItem in global::Scene._popupStack)
            {
                text = text + " " + popupItem.popup.name;
            }
            Debugger.Assert(false, text);
            global::Scene._popupStack.Clear();
        }
    }

    public static void ChangeTo(string sceneName, object param = null)
    {
        if (global::Scene.currentScene == null)
        {
            global::Scene.Init();
        }
        Debugger.Assert(global::Scene._sceneCache.ContainsKey(sceneName), "'" + sceneName + "' scene not found.");
        global::Scene.moveBackward = false;
        TaskManager.StartCoroutine(global::Scene.ChangeSceneCoroutine(sceneName, param));
        FlurryLogSender.Log(sceneName, null);
    }

    public static bool RegistPopup(GameObject popupRoot)
    {
        if (global::Scene._popupCache.ContainsKey(popupRoot.name))
        {
            UnityEngine.Debug.LogError("'" + popupRoot.name + "' is already registered.");
            return false;
        }
        global::Scene._popupCache[popupRoot.name] = popupRoot;
        popupRoot.SetActive(false);
        return true;
    }

    public static bool ShowPopup(string popupName, object param = null, Action<object> onClose = null)
    {
        Debugger.Assert(global::Scene._popupCache.ContainsKey(popupName), "'" + popupName + "' popup not found.");
        GameObject gameObject = global::Scene._popupCache[popupName];
        foreach (global::Scene.PopupItem popupItem in global::Scene._popupStack)
        {
            Debugger.Assert(!popupItem.popup.Equals(gameObject), "'" + gameObject.name + "' popup aleady shown.");
        }
        global::Scene.currentPopup = gameObject;
        GameObject prevPopup = (global::Scene._popupStack.Count <= 0) ? null : global::Scene._popupStack.Peek().popup;
        global::Scene.PopupItem popupItem2 = new global::Scene.PopupItem(gameObject, param, onClose);
        global::Scene._popupStack.Push(popupItem2);
        global::Scene.ShowPopupInternal(popupItem2, prevPopup);
        if (global::Scene.onPopupOpened != null)
        {
            global::Scene.onPopupOpened();
        }
        return true;
    }

    public static void QueuePopup(string popupName)
    {
        global::Scene.QueuePopup(popupName, null, null);
    }

    public static void QueuePopup(string popupName, object param)
    {
        global::Scene.QueuePopup(popupName, param, null);
    }

    public static void QueuePopup(string popupName, object param, Action<object> onClose)
    {
        Debugger.Assert(global::Scene._popupCache.ContainsKey(popupName), "'" + popupName + "' popup not found.");
        GameObject gameObject = global::Scene._popupCache[popupName];
        foreach (global::Scene.PopupItem popupItem in global::Scene._popupStack)
        {
            Debugger.Assert(!popupItem.popup.Equals(gameObject), "'" + gameObject.name + "' popup aleady shown.");
        }
        if (global::Scene._popupStack.Count > 0)
        {
            global::Scene._popupStack.Peek().Append(new global::Scene.PopupItem(gameObject, param, onClose));
        }
        else
        {
            global::Scene.ShowPopup(popupName, param, onClose);
        }
    }

    private static void ShowPopupInternal(global::Scene.PopupItem item, GameObject prevPopup)
    {
        item.hasShown = true;
        GameObject popup = item.popup;
        object param = item.param;
        UnityEngine.Debug.Log(">>>>>>>>>>>>>>> [ " + popup.name + " ]");
        bool flag = false;
        //if (!UncleBill.IsRich && Context.AdManager.IsPossibleToShowInterstitial(popup.name, true))
        //{
        //    flag = true;
        //}
        if (flag)
        {
            global::Scene.ShowInterstitialWithSceneLock(popup, true, delegate (bool success)
            {
                TaskManager.StartCoroutine(global::Scene.CoShowPopupInternal(popup, param, prevPopup, true));
            });
        }
        else
        {
            TaskManager.StartCoroutine(global::Scene.CoShowPopupInternal(popup, param, prevPopup, false));
        }
    }

    private static IEnumerator CoShowPopupInternal(GameObject popup, object param, GameObject prevPopup, bool skipAnim)
    {
        global::Scene.PopupShowAnimationCount++;
        Debugger.Assert(popup != null, "popup is null");
        Camera popupCamera = global::Scene.GetCamera(popup);
        Debugger.Assert(popupCamera != null, "popupCamera is null");
        if (prevPopup != null)
        {
            popupCamera.depth = global::Scene.GetCamera(prevPopup).depth + 1f;
            global::Scene.SetCurtain(prevPopup, true);
        }
        else
        {
            popupCamera.depth = global::Scene.popupBaseDepth;
            global::Scene.SetCurtain(global::Scene.currentScene, true);
            foreach (GameObject gameObject in global::Scene._overlayCache.Values)
            {
                if (gameObject.activeSelf)
                {
                    global::Scene.SetCurtain(gameObject, true);
                }
            }
        }
        popup.SetActive(true);
        Debugger.AssertInspector(global::Scene.GetHandler(popup));
        popup.SendMessage("DoDataExchange");
        popup.SendMessage("OnEnterPopup", (param != null) ? param : popup);
        IAnimationHandler ah = popup.GetComponent(typeof(IAnimationHandler)) as IAnimationHandler;
        if (!skipAnim && ah != null)
        {
            yield return TaskManager.StartCoroutine(ah.CoShowAnimation());
        }
        if (global::Scene.currentPopup == popup)
        {
            global::Scene.SetCurtain(popup, false);
        }
        global::Scene.PopupShowAnimationCount--;
        popup.SendMessage("OnDidEnterPopup", SendMessageOptions.DontRequireReceiver);
        FlurryLogSender.Log(popup.name, null);
        yield break;
    }

    public static bool ClosePopup(string popupName, object param = null)
    {
        Debugger.Assert(global::Scene._popupCache.ContainsKey(popupName), "'" + popupName + "' is not exist.");
        if (global::Scene.IsPopupClosing)
        {
            UnityEngine.Debug.LogWarning("Scene.ClosePopup : Another popup is closing.");
            return false;
        }
        if (global::Scene.PopupShowAnimationCount > 0)
        {
            UnityEngine.Debug.LogWarning("Scene.ClosePopup : Another popup is showing.");
            return false;
        }
        if (global::Scene.currentPopup == null)
        {
            return false;
        }
        Debugger.Assert(global::Scene.currentPopup.name == popupName, "'" + popupName + "' is not current popup. Current is " + global::Scene.currentPopup.name);
        Debugger.Assert(global::Scene.currentPopup == global::Scene._popupStack.Peek().popup, "Popup stack piled wrong.");
        global::Scene.PopupItem popupItem = global::Scene._popupStack.Pop();
        GameObject prevPopup = (global::Scene._popupStack.Count <= 0) ? null : global::Scene._popupStack.Peek().popup;
        global::Scene.PopupItem prevItem = new global::Scene.PopupItem(popupItem);
        if (popupItem.HasNext)
        {
            popupItem.Next();
            global::Scene._popupStack.Push(popupItem);
        }
        global::Scene.currentPopup = ((global::Scene._popupStack.Count <= 0) ? null : global::Scene._popupStack.Peek().popup);
        global::Scene.ClosePopupInternal(prevItem.popup, delegate
        {
            if (prevItem.callback != null)
            {
                prevItem.callback(param);
            }
            if (global::Scene._popupStack.Count > 0)
            {
                global::Scene.PopupItem popupItem2 = global::Scene._popupStack.Peek();
                if (!popupItem2.hasShown)
                {
                    global::Scene.ShowPopupInternal(popupItem2, prevPopup);
                }
            }
        });
        if (global::Scene.onPopupClosed != null)
        {
            global::Scene.onPopupClosed();
        }
        return true;
    }

    private static void ShowInterstitialWithSceneLock(GameObject popup, bool isIn, Action<bool> interstitialClose)
    {
        Context.AdManager.ShowInterstitial(popup.name, false, interstitialClose);
    }

    private static void ClosePopupInternal(GameObject popup, Action callback)
    {
        Debugger.Assert(!global::Scene.IsPopupClosing, "Scene.ClosePopup : Another popup is closing.");
        UnityEngine.Debug.Log("<<<<<<<<<<<<<<< [ " + popup.name + " ]");
        bool flag = false;
        //if (!UncleBill.IsRich && Context.AdManager.IsPossibleToShowInterstitial(popup.name, false))
        //{
        //    flag = true;
        //}
        if (flag)
        {
            global::Scene.ShowInterstitialWithSceneLock(popup, false, delegate (bool success)
            {
                TaskManager.StartCoroutine(global::Scene.CoClosePopupInternal(popup, true, callback));
            });
        }
        else
        {
            TaskManager.StartCoroutine(global::Scene.CoClosePopupInternal(popup, false, callback));
        }
    }

    private static IEnumerator CoClosePopupInternal(GameObject popup, bool skipAnim, Action callback)
    {
        global::Scene.IsPopupClosing = true;
        global::Scene.SetCurtain(popup, true);
        popup.SendMessage("OnWillLeavePopup", SendMessageOptions.DontRequireReceiver);
        IAnimationHandler ah = popup.GetComponent(typeof(IAnimationHandler)) as IAnimationHandler;
        if (!skipAnim && ah != null)
        {
            yield return TaskManager.StartCoroutine(ah.CoHideAnimation());
        }
        popup.SendMessage("OnLeavePopup", SendMessageOptions.DontRequireReceiver);
        popup.SetActive(false);
        if (global::Scene._popupStack.Count > 0)
        {
            global::Scene.SetCurtain(global::Scene._popupStack.Peek().popup, false);
        }
        else
        {
            global::Scene.SetCurtain(global::Scene.currentScene, false);
            foreach (GameObject gameObject in global::Scene._overlayCache.Values)
            {
                if (gameObject.activeSelf)
                {
                    global::Scene.SetCurtain(gameObject, false);
                }
            }
        }
        global::Scene.IsPopupClosing = false;
        if (callback != null)
        {
            callback();
        }
        yield break;
    }

    public static void ShowOverlay(string overlayName, object param = null)
    {
        Debugger.Assert(global::Scene._overlayCache.ContainsKey(overlayName), "'" + overlayName + "' overlay not found.");
        GameObject gameObject = global::Scene._overlayCache[overlayName];
        gameObject.SetActive(true);
        Debugger.AssertInspector(global::Scene.GetHandler(gameObject));
        gameObject.SendMessage("DoDataExchange");
        gameObject.SendMessage("OnEnterUIOveray", (param != null) ? param : gameObject);
        global::Scene.SetCurtain(gameObject, false);
    }

    public static void CloseOverlay(string overlayName)
    {
        Debugger.Assert(global::Scene._overlayCache.ContainsKey(overlayName), "'" + overlayName + "' overlay not found.");
        GameObject gameObject = global::Scene._overlayCache[overlayName];
        gameObject.SendMessage("OnLeaveUIOveray");
        gameObject.SetActive(false);
        global::Scene.SetCurtain(gameObject, true);
    }

    public static bool Back(object param = null)
    {
        if (global::Scene._sceneHistory.Count <= 0)
        {
            return false;
        }
        global::Scene.moveBackward = true;
        string sceneName = global::Scene._sceneHistory.Pop();
        TaskManager.StartCoroutine(global::Scene.ChangeSceneCoroutine(sceneName, param));
        return true;
    }

    public static void ClearHistory()
    {
        global::Scene._sceneHistory.Clear();
    }

    public static void AddHistory(string name)
    {
        global::Scene._sceneHistory.Add(name);
    }

    public static bool IsPopupOpen()
    {
        return null != global::Scene.currentPopup;
    }

    public static bool IsPopupOpen(string popupName)
    {
        foreach (global::Scene.PopupItem popupItem in global::Scene._popupStack)
        {
            if (popupItem.popup.name == popupName)
            {
                return true;
            }
        }
        return false;
    }

    public static GameObject CurrentScene()
    {
        return global::Scene.currentScene;
    }

    public static GameObject CurrentPopup()
    {
        return global::Scene.currentPopup;
    }

    public static bool SendMessage<T>(string messageName, object param = null, SendMessageOptions options = SendMessageOptions.RequireReceiver) where T : IHandlerBase
    {
        bool result = false;
        if (global::Scene.currentScene != null && global::Scene.currentScene.GetComponent(typeof(T)) != null)
        {
            global::Scene.currentScene.SendMessage(messageName, param, options);
            result = true;
        }
        foreach (global::Scene.PopupItem popupItem in global::Scene._popupStack)
        {
            if (popupItem.popup.activeSelf)
            {
                if (!(popupItem.popup.GetComponent(typeof(T)) == null))
                {
                    popupItem.popup.SendMessage(messageName, param, options);
                    result = true;
                }
            }
        }
        foreach (GameObject gameObject in global::Scene._overlayCache.Values)
        {
            if (gameObject.activeSelf)
            {
                if (!(gameObject.GetComponent(typeof(T)) == null))
                {
                    gameObject.SendMessage(messageName, param, options);
                    result = true;
                }
            }
        }
        return result;
    }

    public static GameObject FindInChildren(this GameObject go, string name)
    {
        foreach (Transform transform in go.GetComponentsInChildren<Transform>())
        {
            if (transform.gameObject.name == name)
            {
                return transform.gameObject;
            }
        }
        return null;
    }

    public static Camera FindCameraForObject(GameObject go)
    {
        Transform transform = go.transform;
        while (transform != null)
        {
            MonoBehaviour handler = global::Scene.GetHandler(transform.gameObject);
            if (handler != null)
            {
                transform = handler.transform;
                Camera componentInChildren = transform.GetComponentInChildren<Camera>();
                if (componentInChildren != null)
                {
                    return componentInChildren;
                }
            }
            transform = transform.parent;
        }
        return null;
    }

    public static bool IsSceneRoot(GameObject go)
    {
        return global::Scene.HasHandlerTyped(go, typeof(IGameScene));
    }

    public static bool HasHandlerTyped(GameObject go, Type type)
    {
        MonoBehaviour[] components = go.GetComponents<MonoBehaviour>();
        foreach (MonoBehaviour monoBehaviour in components)
        {
            if (monoBehaviour != null && type.IsAssignableFrom(monoBehaviour.GetType()))
            {
                return true;
            }
        }
        return false;
    }

    public static MonoBehaviour GetHandler(GameObject go)
    {
        MonoBehaviour[] components = go.GetComponents<MonoBehaviour>();
        foreach (MonoBehaviour monoBehaviour in components)
        {
            if (monoBehaviour != null && typeof(IHandlerBase).IsAssignableFrom(monoBehaviour.GetType()))
            {
                return monoBehaviour;
            }
        }
        return null;
    }

    public static MonoBehaviour GetHandlerTyped(GameObject go, Type type)
    {
        MonoBehaviour[] components = go.GetComponents<MonoBehaviour>();
        foreach (MonoBehaviour monoBehaviour in components)
        {
            if (monoBehaviour != null && type.IsAssignableFrom(monoBehaviour.GetType()))
            {
                return monoBehaviour;
            }
        }
        return null;
    }

    public static GameObject FindHandler(GameObject go, Type type)
    {
        Transform transform = go.transform;
        while (transform != null)
        {
            if (global::Scene.HasHandlerTyped(transform.gameObject, type))
            {
                return transform.gameObject;
            }
            transform = transform.parent;
        }
        return null;
    }

    public static GameObject FindSceneRoot(GameObject go)
    {
        return global::Scene.FindHandler(go, typeof(IGameScene));
    }

    public static Bounds GetRenderBound(GameObject go)
    {
        Bounds result = new Bounds(go.transform.position, Vector3.zero);
        foreach (Renderer renderer in go.GetComponentsInChildren<Renderer>())
        {
            result.Encapsulate(renderer.bounds);
        }
        return result;
    }

    public static Bounds GetBound(GameObject go)
    {
        Bounds result = new Bounds(go.transform.position, Vector3.zero);
        foreach (Renderer renderer in go.GetComponentsInChildren<Renderer>())
        {
            result.Encapsulate(renderer.bounds);
        }
        foreach (Collider collider in go.GetComponentsInChildren<Collider>())
        {
            result.Encapsulate(collider.bounds);
        }
        result.center -= go.transform.position;
        return result;
    }

    public static Vector2 GetCameraSizeInWorldSpace(Camera cam)
    {
        tk2dCamera component = cam.GetComponent<tk2dCamera>();
        if (component == null)
        {
            return new Vector2(cam.orthographicSize * cam.aspect * 2f, cam.orthographicSize * 2f);
        }
        return new Vector2(component.ScreenExtents.width, component.ScreenExtents.height);
    }

    public static void Lock()
    {
        global::Scene.Lock(false, string.Empty);
    }

    public static void Lock(bool enableCurtain)
    {
        global::Scene.Lock(enableCurtain, "Loading...");
    }

    public static void Lock(string message)
    {
        global::Scene.Lock(true, message);
    }

    public static void Lock(bool enableCurtain, string message)
    {
        UnityEngine.Debug.Log("Scene.Lock");
        if (global::Scene.currentPopup != null)
        {
            global::Scene.SetCurtain(global::Scene.currentPopup, true);
        }
        else if (global::Scene.currentScene != null)
        {
            global::Scene.SetCurtain(global::Scene.currentScene, true);
            foreach (GameObject gameObject in global::Scene._overlayCache.Values)
            {
                if (gameObject.activeSelf)
                {
                    global::Scene.SetCurtain(gameObject, true);
                }
            }
        }
        if (enableCurtain)
        {
            IndicatorWithMessage.Show(message);
        }
        global::Scene.IsLocked = true;
    }

    public static void Unlock()
    {
        UnityEngine.Debug.Log("Scene.UnLock");
        if (global::Scene.currentPopup != null)
        {
            global::Scene.SetCurtain(global::Scene.currentPopup, false);
        }
        else if (global::Scene.currentScene != null)
        {
            global::Scene.SetCurtain(global::Scene.currentScene, false);
            foreach (GameObject gameObject in global::Scene._overlayCache.Values)
            {
                if (gameObject.activeSelf)
                {
                    global::Scene.SetCurtain(gameObject, false);
                }
            }
        }
        IndicatorWithMessage.Hide();
        global::Scene.IsLocked = false;
    }

    public static void BlockBanner(Camera cam)
    {
        Debugger.Assert(cam != null);
        if (cam == null)
        {
            return;
        }
        float num = 0.078125f;
        tk2dCamera component = cam.GetComponent<tk2dCamera>();
        BoxCollider boxCollider = cam.GetComponent<BoxCollider>();
        if (boxCollider == null)
        {
            boxCollider = cam.gameObject.AddComponent<BoxCollider>();
        }
        if (component == null)
        {
            boxCollider.size = new Vector3(cam.orthographicSize * cam.aspect * 2f, cam.orthographicSize * 2f * num, 0.1f);
            boxCollider.center = new Vector3(0f, (cam.orthographicSize - cam.orthographicSize * 2f * num / 2f) * -1f, 1f);
        }
        else
        {
            boxCollider.size = new Vector3(component.ScreenExtents.width, component.ScreenExtents.height * num, 1f);
            if (component.CameraSettings.orthographicOrigin == tk2dCameraSettings.OrthographicOrigin.BottomLeft)
            {
                boxCollider.center = new Vector3(0f, (component.ScreenExtents.height * 0.5f - component.ScreenExtents.height * num / 2f) * -1f, 1f);
            }
            else
            {
                boxCollider.center = new Vector3(0f, (component.ScreenExtents.height - component.ScreenExtents.height * num / 2f) * -1f, 1f);
            }
        }
        boxCollider.enabled = true;
    }

    private static void Init()
    {
        global::Scene.InitCache<IGameScene>("GameScene", global::Scene._sceneCache, delegate (GameObject scene)
        {
            global::Scene.popupBaseDepth = Mathf.Max(global::Scene.popupBaseDepth, global::Scene.GetCamera(scene).depth);
        });
        global::Scene.InitCache<IPopupWnd>("PopupWnd", global::Scene._popupCache, null);
        global::Scene.InitCache<IUIOverlay>("UIOverlay", global::Scene._overlayCache, delegate (GameObject overlay)
        {
            global::Scene.popupBaseDepth = Mathf.Max(global::Scene.popupBaseDepth, global::Scene.GetCamera(overlay).depth);
        });
        global::Scene.popupBaseDepth = (float)((int)(global::Scene.popupBaseDepth + 1f));
    }

    private static void InitCache<T>(string objName, Dictionary<string, GameObject> dict, Action<GameObject> eachAction = null) where T : IHandlerBase
    {
        List<GameObject> list = new List<GameObject>();
        foreach (GameObject gameObject in UnityEngine.Object.FindObjectsOfType<GameObject>())
        {
            if (gameObject.name.Contains(objName))
            {
                list.Add(gameObject);
            }
        }
        Debugger.Assert(list.Count == 1, "Root must be one : " + objName);
        dict.Clear();
        for (int j = 0; j < list[0].transform.childCount; j++)
        {
            GameObject gameObject2 = list[0].transform.GetChild(j).gameObject;
            Debugger.Assert(gameObject2.GetComponent(typeof(T)) != null, "Cant found handler - " + gameObject2.name);
            dict[gameObject2.name] = gameObject2;
            gameObject2.SetActive(false);
            if (eachAction != null)
            {
                eachAction(gameObject2);
            }
        }
    }

    private static void SetCurtain(GameObject sceneMember, bool isOn)
    {
        Debugger.Assert(sceneMember != null, "sceneMember is null");
        GameObject curtain = global::Scene.GetCurtain(sceneMember);
        Debugger.Assert(curtain != null, "curtain is null");
        Camera camera = global::Scene.GetCamera(sceneMember);
        Debugger.Assert(camera != null, "cam is null");
        tk2dCamera component = camera.GetComponent<tk2dCamera>();
        BoxCollider component2 = curtain.GetComponent<BoxCollider>();
        Debugger.Assert(component2 != null, "boxCol is null");
        if (isOn)
        {
            if (component == null)
            {
                component2.size = new Vector3(camera.orthographicSize * camera.aspect * 2f, camera.orthographicSize * 2f, 1f) * 1.2f;
            }
            else
            {
                Vector3 size = new Vector3(component.ScreenExtents.width, component.ScreenExtents.height, 1f) * 1.2f;
                component2.size = size;
                tk2dCameraSettings tk2dCameraSettings = (!(component.InheritConfig == null)) ? component.InheritConfig.CameraSettings : component.CameraSettings;
                if (tk2dCameraSettings.orthographicOrigin == tk2dCameraSettings.OrthographicOrigin.BottomLeft)
                {
                    component2.center = new Vector3(component.ScreenExtents.width * 0.5f, component.ScreenExtents.height * 0.5f, 0f);
                }
            }
        }
        sceneMember.SendMessage("OnCurtainActive", isOn, SendMessageOptions.DontRequireReceiver);
        curtain.gameObject.SetActive(isOn);
    }

    private static GameObject GetCurtain(GameObject sceneMember)
    {
        Debugger.Assert(sceneMember.GetComponent(typeof(IHandlerBase)) != null);
        Transform transform = sceneMember.transform.Find("curtain");
        Debugger.Assert(transform != null && transform.GetComponent<BoxCollider>() != null);
        return transform.gameObject;
    }

    private static Camera GetCamera(GameObject sceneMember)
    {
        Debugger.Assert(sceneMember != null, "Scene.GetCamera sceneMember is null");
        Debugger.Assert(sceneMember.GetComponent(typeof(IHandlerBase)) != null);
        Transform transform = sceneMember.transform.Find("cam");
        Debugger.Assert(transform != null);
        Camera component = transform.GetComponent<Camera>();
        Debugger.Assert(component != null);
        return component;
    }

    private static void OnAndroidEscape()
    {
        global::Scene.passTime += Time.deltaTime;
        if (global::Scene.IsLocked)
        {
            return;
        }
        if ((Application.platform == RuntimePlatform.Android || Application.isEditor) && UnityEngine.Input.GetKeyUp(KeyCode.Escape) && global::Scene.passTime >= 1f)
        {
            global::Scene.passTime = 0f;
            global::Scene.Escape();
        }
    }

    private static void Escape()
    {
        if (global::Scene.currentPopup != null)
        {
            global::Scene.currentPopup.SendMessage("OnEscape", SendMessageOptions.DontRequireReceiver);
        }
        else if (global::Scene.currentScene != null)
        {
            global::Scene.currentScene.SendMessage("OnEscape", SendMessageOptions.DontRequireReceiver);
        }
        else
        {
            UnityEngine.Debug.LogWarning("Escape : Does not response any scene or popup.");
        }
    }

    public static void OnWillLeaveScene()
    {
        global::Scene.SetCurtain(global::Scene.currentScene, true);
        global::Scene.currentScene.SendMessage("OnWillLeaveScene", SendMessageOptions.DontRequireReceiver);
    }

    public static void OnDidLeaveScene(GameObject nextScene)
    {
        global::Scene.currentScene.SendMessage("OnLeaveScene");
        global::Scene.currentScene.SetActive(false);
        if (!global::Scene.moveBackward && (global::Scene.allowOverLap || global::Scene.currentScene != nextScene))
        {
            global::Scene._sceneHistory.Add(global::Scene.currentScene.name);
        }
        if (global::Scene.onSceneLeaved != null)
        {
            global::Scene.onSceneLeaved();
        }
    }

    public static void OnWillEnterScene(object param)
    {
        global::Scene.currentScene.SetActive(true);
        Debugger.AssertInspector(global::Scene.GetHandler(global::Scene.currentScene));
        global::Scene.currentScene.SendMessage("DoDataExchange");
        global::Scene.currentScene.SendMessage("OnEnterScene", (param != null) ? param : global::Scene.currentScene);
        PlayerPrefs.Save();
        if (global::Scene.onSceneEntered != null)
        {
            global::Scene.onSceneEntered();
        }
    }

    public static void OnDidEnterScene()
    {
        global::Scene.currentScene.SendMessage("OnDidEnterScene", SendMessageOptions.DontRequireReceiver);
        if (!global::Scene.IsPopupOpen())
        {
            global::Scene.SetCurtain(global::Scene.currentScene, false);
        }
        if (!global::Scene.hasEnteredFirstScene)
        {
            float realtimeSinceStartup = Time.realtimeSinceStartup;
            DWATTHelper.logEvent("BootTime", new object[]
            {
                realtimeSinceStartup.ToString(),
                string.Empty
            });
            PlayerPrefs.SetFloat("boottime", realtimeSinceStartup);
            global::Scene.hasEnteredFirstScene = true;
        }
    }

    private static Transition CreateTransition(string sceneName, bool isEnter = true)
    {
        Transition transition = global::Scene._transition;
        //if (!UncleBill.IsRich)
        //{
        //    string text = null;
        //    bool isEnter_ = true;
        //    if (Context.AdManager.IsPossibleToShowInterstitial(sceneName, true) && isEnter)
        //    {
        //        text = sceneName;
        //        isEnter_ = true;
        //    }
        //    else if (global::Scene.currentScene != null && Context.AdManager.IsPossibleToShowInterstitial(global::Scene.currentScene.name, false))
        //    {
        //        text = global::Scene.currentScene.name;
        //        isEnter_ = false;
        //    }
        //    if (text != null)
        //    {
        //        transition = global::Scene._transitionDic["Interstitial"];
        //        ((InterstitialTransition)transition).SetCond(text, isEnter_);
        //    }
        //}
        return transition;
    }

    private static IEnumerator ChangeSceneCoroutine(string sceneName, object param = null)
    {
        UnityEngine.Debug.Log("=============== [ " + sceneName + " ] ===============");
        Debugger.Assert(global::Scene._sceneCache.ContainsKey(sceneName), "'" + sceneName + "' scene not found.");
        GameObject nextScene = global::Scene._sceneCache[sceneName];
        Transition curTransition = global::Scene.CreateTransition(sceneName, true);
        if (global::Scene.currentScene != null)
        {
            yield return TaskManager.StartCoroutine(curTransition.TranslateOut(global::Scene.SceneChangeTime * 0.5f, nextScene));
        }
        global::Scene.currentScene = nextScene;
        yield return TaskManager.StartCoroutine(curTransition.TranslateIn(global::Scene.SceneChangeTime * 0.5f, param));
        yield break;
    }

    private static IEnumerator LoadAdditiveScene(string sname, float minTranstionSec)
    {
        string curScene = SceneManager.GetActiveScene().name;
        AsyncOperation _async = SceneManager.LoadSceneAsync(sname, LoadSceneMode.Additive);
        if (minTranstionSec > 0f)
        {
            _async.allowSceneActivation = false;
            float loadingTime = 0f;
            float minProgress = 0f;
            while (minTranstionSec > 0f && minProgress < 0.9f)
            {
                global::Scene.LoadingProgress = Mathf.Clamp01(minProgress * 1.11111f);
                yield return null;
                loadingTime += Time.deltaTime;
                minProgress = Mathf.Min(loadingTime / minTranstionSec, _async.progress);
            }
            _async.allowSceneActivation = true;
            global::Scene.LoadingProgress = 1f;
        }
        while (!_async.isDone)
        {
            yield return null;
        }
        UnityEngine.SceneManagement.Scene nextScene = SceneManager.GetSceneByName(sname);
        if (nextScene.IsValid())
        {
            if (global::Scene.currentScene != null)
            {
                yield return TaskManager.StartCoroutine(global::Scene._transition.TranslateOut(global::Scene.SceneChangeTime * 0.5f, null));
                global::Scene.currentScene = null;
            }
            SceneManager.SetActiveScene(nextScene);
            BMPool.Clear();
            HOTween.Complete();
            SceneManager.UnloadScene(curScene);
            Resources.UnloadUnusedAssets();
        }
        else
        {
            UnityEngine.Debug.LogError("nextScene IsValid false");
        }
        yield break;
    }

    private static IEnumerator ChangeUnitySceneCoroutine(string uSceneName, string sceneName, object param = null, string loadingScene = "Loading")
    {
        if (global::Scene.currentScene != null)
        {
            Transition outTransition = global::Scene.CreateTransition(string.Empty, false);
            yield return TaskManager.StartCoroutine(outTransition.CoTransitionOut(0f));
        }
        global::Scene.LoadingProgress = 0f;
        if (loadingScene != "PASS")
        {
            yield return TaskManager.StartCoroutine(global::Scene.LoadAdditiveScene(loadingScene, 0f));
        }
        yield return TaskManager.StartCoroutine(global::Scene.LoadAdditiveScene(uSceneName, global::Scene.SceneChangeTime * 2f));
        Debugger.Assert(global::Scene.currentScene == null);
        global::Scene.ChangeTo(sceneName, param);
        if (global::Scene.currentScene != null)
        {
            Transition inTransition = global::Scene.CreateTransition(sceneName, true);
            yield return TaskManager.StartCoroutine(inTransition.CoTransitionOut(0f));
        }
        yield break;
    }

    private static void TurnOnCamera(GameObject scene, bool isOn)
    {
        Camera camera = global::Scene.FindCameraForObject(scene);
        camera.enabled = isOn;
        global::Scene.SetCurtain(scene, false);
    }

    public static bool SupportsFastBlur()
    {
        return !SystemInfo.graphicsDeviceName.ToLower().Contains("adreno");
    }

    public static void SetBlur(Camera cam, bool on)
    {
        if (global::Scene.SupportsFastBlur())
        {
            Blur blur = cam.GetComponent<Blur>();
            if (on)
            {
                if (blur == null)
                {
                    blur = cam.gameObject.AddComponent<Blur>();
                }
                blur.downsample = 2;
                blur.blurShader = Shader.Find("Hidden/FastBlur");
                blur.blurSize = 0f;
                blur.blurIterations = 1;
                HOTween.Kill(blur);
                HOTween.To(blur, 0.3f, "blurSize", 9);
                HOTween.To(blur, 0.3f, "blurIterations", 3);
                blur.enabled = true;
            }
            else if (blur != null)
            {
                HOTween.Kill(blur);
                HOTween.To(blur, 0.3f, new TweenParms().Prop("blurSize", 0).OnComplete(delegate (TweenEvent twevent)
                {
                    blur.enabled = false;
                }, new object[0]));
                HOTween.To(blur, 0.3f, "blurIterations", 1);
            }
        }
        else if (on)
        {
            if (global::Scene.blurReplacement == null)
            {
                GameObject original = Resources.Load("messageBlock") as GameObject;
                global::Scene.blurReplacement = UnityEngine.Object.Instantiate<GameObject>(original);
                global::Scene.blurReplacement.name = "blurReplacement";
            }
            global::Scene.blurReplacement.SetActive(true);
            MessageBlock component = global::Scene.blurReplacement.GetComponent<MessageBlock>();
            component.fadeTarget = 0.85f;
            component.Show(cam);
        }
        else
        {
            Debugger.Assert(global::Scene.blurReplacement != null);
            if (global::Scene.blurReplacement == null)
            {
                return;
            }
            global::Scene.blurReplacement.SetActive(false);
        }
    }

    public static void SetBlur(Camera cam, bool on, Color color)
    {
        global::Scene.SetBlur(cam, on);
        if (global::Scene.blurReplacement != null)
        {
            MessageBlock component = global::Scene.blurReplacement.GetComponent<MessageBlock>();
            component.SetPlaneColor(color);
        }
    }

    private static GameObject currentScene;

    private static GameObject currentPopup;

    private static GameObject blurReplacement;

    private static bool hasEnteredFirstScene = false;

    private static Dictionary<string, GameObject> _sceneCache = new Dictionary<string, GameObject>();

    private static Dictionary<string, GameObject> _popupCache = new Dictionary<string, GameObject>();

    private static Dictionary<string, GameObject> _overlayCache = new Dictionary<string, GameObject>();

    private static Stack<global::Scene.PopupItem> _popupStack = new Stack<global::Scene.PopupItem>();

    private static CircularQuene<string> _sceneHistory = new CircularQuene<string>(10);

    private const string kGameSceneName = "GameScene";

    private const string kPopupWndName = "PopupWnd";

    private const string kUIOverlayName = "UIOverlay";

    private const float ESCAPE_TIME_INTERVAL = 1f;

    private static float popupBaseDepth = 0f;

    private static float sceneChangeTime = 1f;

    private static float passTime = 0f;

    private static bool enableAndroidEscapeEvent = false;

    public static bool moveBackward = false;

    private static Transition _transition;

    public static bool allowOverLap = true;

    public static Action onSceneEntered;

    public static Action onSceneLeaved;

    public static Action onPopupOpened;

    public static Action onPopupClosed;

    private static Dictionary<string, Transition> _transitionDic = new Dictionary<string, Transition>
    {
        {
            "Fade",
            new FadeTransition()
        },
        {
            "Delay",
            new DelayTransition()
        },
        {
            "Spotlight",
            new SpotlightTransition()
        },
        {
            "StarSpotlight",
            new StarSpotlightTransition()
        },
        {
            "HexagonSpotlight",
            new HexagonSpotlightTransition()
        },
        {
            "Custom",
            new CustomTransition()
        },
        {
            "Interstitial",
            new InterstitialTransition()
        }
    };

    [CompilerGenerated]
    private static Action _003C_003Ef__mg_0024cache0;

    [CompilerGenerated]
    private static Action _003C_003Ef__mg_0024cache1;

    [CompilerGenerated]
    private static Action _003C_003Ef__mg_0024cache2;

    private class PopupItem
    {
        public PopupItem(global::Scene.PopupItem item)
        {
            item.CopyTo(this);
        }

        public PopupItem(GameObject _popup, object _param, Action<object> _callback)
        {
            this.popup = _popup;
            this.param = _param;
            this.callback = _callback;
            this.hasShown = false;
        }

        public bool HasNext
        {
            get
            {
                return this.queue.Count > 0;
            }
        }

        public void Append(global::Scene.PopupItem item)
        {
            this.queue.Enqueue(item);
        }

        public void Next()
        {
            if (this.queue.Count == 0)
            {
                return;
            }
            this.hasShown = false;
            this.queue.Dequeue().CopyTo(this);
        }

        public void CopyTo(global::Scene.PopupItem item)
        {
            item.popup = this.popup;
            item.param = this.param;
            item.callback = this.callback;
            item.hasShown = this.hasShown;
        }

        public GameObject popup;

        public bool hasShown;

        public object param;

        public Action<object> callback;

        public Queue<global::Scene.PopupItem> queue = new Queue<global::Scene.PopupItem>();
    }
}
